﻿using System;
using Newtonsoft.Json;

namespace ZilLion.TPO2O.SDK.EntityBase
{
    public class RequestBodyWrapper
    {
        public RequestBodyWrapper(byte[] bytesbody)
        {
            RequestBodyString = bytesbody.ToBase64String();
            RequestBodyType = RequestBodyType.ByteArrayBody;
        }

        public RequestBodyWrapper(string stringbody)
        {
            RequestBodyString = stringbody;
#if !DEBUG
        RequestBodyString = stringbody.EncodeBase64();
#endif
            RequestBodyType = RequestBodyType.StringBody;
        }


        public RequestBodyType RequestBodyType { get; set; }

        public string RequestBodyString { get; set; }


        public string GetBodyString()
        {
            var result = RequestBodyString;
#if !DEBUG
            result = RequestBodyString.DecodeBase64();
#endif
            return result;
        }

        public byte[] GetBodyByteArray()
        {
            return Convert.FromBase64String(RequestBodyString);
        }


        public T GetBodyObject<T>() where T : class
        {
            var bodystring = GetBodyString();
            try
            {
                return JsonConvert.DeserializeObject<T>(bodystring);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }


    public enum RequestBodyType
    {
        StringBody,
        ByteArrayBody
    }
}